/*!
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *    Author: flareguner
 *    E-Mail: flareguner@gmail.com
 *    Years: 2010-2011
 */


#include "historyconfig.h"
#include "src/platform.h"

#include <QtCore/QSettings>
#include <QtCore/QEvent>
#include <QtGui/QCheckBox>
#include <QtGui/QFrame>
#include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>
#include <QtGui/QApplication>

//Defaults

const bool
HistoryConfig::m_default_enabled = true,
HistoryConfig::m_default_dont_store_if_match = true;






HistoryConfig::HistoryConfig(QWidget *parent) :
    QWidget(parent)
{
    createUi();    changeEvent(new QEvent(QEvent::LanguageChange));



    connect(enableHistory, SIGNAL(toggled(bool)),frame, SLOT(setEnabled(bool)));

    read();
}

HistoryConfig::~HistoryConfig() {
    delete enableHistory;
    delete dontStoreIfMatch;
//    delete frame;
    delete frameLayout;
    delete mainLayout;
}

void HistoryConfig::save() {
    QSettings s(Platform::getOrganizationName(), Platform::getApplicationName());
    s.beginGroup("History");
    s.setValue("enabled",enableHistory->isChecked());
    s.setValue("dontStoreIfMatch", dontStoreIfMatch->isChecked());
    s.endGroup();
}

void HistoryConfig::read() {
    QSettings s(Platform::getOrganizationName(), Platform::getApplicationName());
    s.beginGroup("History");
    enableHistory->setChecked(s.value("enabled", m_default_enabled).toBool());
    dontStoreIfMatch->setChecked(s.value("dontStoreIfMatch", m_default_dont_store_if_match).toBool());
    s.endGroup();
}

void HistoryConfig::truncate() {
    enableHistory->setChecked(m_default_enabled);
    dontStoreIfMatch->setChecked(m_default_dont_store_if_match);
}


bool HistoryConfig::getHistoryEnabled() const {
    return enableHistory->isChecked();
}

bool HistoryConfig::getStoreIfMatch() const {
    return !dontStoreIfMatch->isChecked();
}

void HistoryConfig::changeEvent(QEvent *e) {
    if(e->type() == QEvent::LanguageChange) {
	enableHistory->setText(QApplication::tr("Enable history"));
	dontStoreIfMatch->setText(QApplication::tr("Don\'t store if match"));
	dontStoreIfMatch->setToolTip(QApplication::tr("Don\'t store if result match with source"));
    }
}

void HistoryConfig::createUi() {
    enableHistory = new QCheckBox(this);
    dontStoreIfMatch = new QCheckBox(this);

    frame = new QFrame(this);

    frameLayout = new QHBoxLayout;
    mainLayout = new QVBoxLayout;

    frameLayout->addWidget(dontStoreIfMatch);
    frame->setLayout(frameLayout);
    frameLayout->setContentsMargins(14,4,4,4);

    mainLayout->addWidget(enableHistory);
    mainLayout->addWidget(frame);
    mainLayout->addStretch();

    this->setLayout(mainLayout);
}
